package com.wzl.customviewproject.page;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager2.widget.ViewPager2;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.wzl.customviewproject.R;
import com.wzl.customviewproject.adapter.FragmentsAdapter;
import com.wzl.customviewproject.adapter.ParentListAdapter;

import java.util.ArrayList;

public class MallHomePageActivity extends AppCompatActivity {
    private TabLayoutMediator mediator;
    private TabLayout tabLayout;
    private ViewPager2 viewPager2;
    private int activeColor = Color.parseColor("#ff678f");
    private int normalColor = Color.parseColor("#666666");
    String[] tabs = new String[]{"关注", "推荐", "最新"};
    private int activeSize = 20;
    private int normalSize = 14;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mall_home_page);
        SwipeRefreshLayout refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.setRefreshing(false);
            }
        });
        RecyclerView rvParent = findViewById(R.id.list_parent);
        rvParent.setLayoutManager(new LinearLayoutManager(this));
        ArrayList<String> parentList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            parentList.add("ParentView"+i);
        }
        ParentListAdapter parentListAdapter = new ParentListAdapter(this, parentList);
        rvParent.setAdapter(parentListAdapter);

        viewPager2 = findViewById(R.id.viewpager_view);
        FragmentsAdapter fragmentsAdapter = new FragmentsAdapter(getSupportFragmentManager(),getLifecycle());
        viewPager2.setAdapter(fragmentsAdapter);

        tabLayout = findViewById(R.id.tableLayout);

        //viewPager 页面切换监听
        viewPager2.registerOnPageChangeCallback(changeCallback);



        mediator = new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //这里可以自定义TabView
                TextView tabView = new TextView(MallHomePageActivity.this);
                TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tabView.setLayoutParams(layoutParams);

                int[][] states = new int[2][];
                states[0] = new int[]{android.R.attr.state_selected};
                states[1] = new int[]{};

                int[] colors = new int[]{activeColor, normalColor};
                ColorStateList colorStateList = new ColorStateList(states, colors);
                tabView.setText(tabs[position]);
                tabView.setTextSize(normalSize);
                tabView.setGravity(Gravity.CENTER);
                tabView.setTextColor(colorStateList);

                tab.setCustomView(tabView);
            }
        });
        //要执行这一句才是真正将两者绑定起来
        mediator.attach();

    }

    private ViewPager2.OnPageChangeCallback changeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            //可以来设置选中时tab的大小
            int tabCount = tabLayout.getTabCount();
            for (int i = 0; i < tabCount; i++) {
                TabLayout.Tab tab = tabLayout.getTabAt(i);
                TextView tabView = (TextView) tab.getCustomView();
                if (tab.getPosition() == position) {
                    tabView.setTextSize(activeSize);
                    tabView.setGravity(Gravity.CENTER);
                    tabView.setTypeface(Typeface.DEFAULT_BOLD);
                } else {
                    tabView.setTextSize(normalSize);
                    tabView.setGravity(Gravity.CENTER);
                    tabView.setTypeface(Typeface.DEFAULT);
                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        mediator.detach();
        viewPager2.unregisterOnPageChangeCallback(changeCallback);
        super.onDestroy();
    }


}